package com.example.multithread.queue;

import org.apache.juli.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.stream.IntStream;

/**
 * @ClassName QueueDemo
 * @Description TODO
 * @Author leejiliang
 * @DateTime 2020/5/12 7:51 PM
 * @Version 1.0
 */
public class QueueDemo {
	private final static Logger log = LoggerFactory.getLogger(QueueDemo.class);
	public static void main(String[] args) throws InterruptedException {
		BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<>(10);
		DelayQueue<Delayed> delayeds = new DelayQueue<>();
		new Thread(()-> IntStream.rangeClosed(1, 10000000).forEach( i->{
			blockingQueue.add(i);
			log.info("添加了{}, 内部有{}个元素.", i, blockingQueue.size());
			try {
				Thread.sleep(700);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		})).start();
		new Thread(()-> IntStream.rangeClosed(1, 10000000).forEach( i->{
			Integer poll = null;
			try {
				poll = blockingQueue.take();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			log.info("拿到了{}, 内部有{}个元素.", poll, blockingQueue.size());
			try {
				Thread.sleep(900);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		})).start();
		Thread.sleep(1000000);
	}
}
